package com.hugoo.prt.slt;

import com.hugoo.core.Conf;
import com.hugoo.core.db.HSF;
import com.hugoo.core.util.TL;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Lovelock.Chan
 */
public class PrtBuzz {

    private String sid,dbl;
    private String prtName, prtKey, prtParaName, prtParaValue, prtLoca, prtUrl;
    private Map para;

    public PrtBuzz(String sid, Map m) {
        this.sid = sid;
        prtName = Conf.getBaseProp("prt.name");
        prtKey = Conf.getBaseProp("prt.key");
        prtLoca = Conf.getBaseProp("prt.location");
        prtUrl = Conf.getBaseProp("prt.url");
        dbl = Conf.getBaseProp(sid, "prt.dbl");//打印组件的DBL
        prtParaName = Conf.getBaseProp(sid, "prt.para.name");
        prtParaValue = Conf.getBaseProp(sid, "prt.para.value");
        para = m;
        prtUrl = prtUrl.endsWith("/") ? prtUrl : prtUrl + "/";
    }

    public enum Type {

        VIEW, PRINT, EDIT
    }

    public String gen(HttpServletRequest request, Type type) {
        String head = "<html><head></head><body><script>"
                + "window.onload = function() {"
                + "var ObjPrintMange;"
                + "try{ObjPrintMange =  new ActiveXObject('WebPrint.WebPrintUnit'); } catch(e) { if( confirm('打印控件未安装，现在下载吗?') ) { window.location='./prt/PrintActivex.exe'; }return;}";

        StringBuilder sb = new StringBuilder();
        sb.append(head);
        sb.append("ObjPrintMange.CheckReg('").append(prtName).append("', '").append(prtKey).append("');");
        if (type == Type.EDIT) {
            sb.append("ObjPrintMange.ReportFileName='").append(sid).append(".fr3';");
            sb.append("ObjPrintMange.PostURL='").append(prtUrl).append(sid).append(".prt?m=save&__usid__=1cc8f3906fd2c3661f308bfcbea88eaa&__jsid__=58E50F43BD8847B631B70A42F867088';");
            sb.append("ObjPrintMange.DesignReport('").append(readTplFile()).append("','").append(prtParaName).append("','").append(prtParaValue).
                    append("'");
        } else if (type == Type.PRINT) {
            sb.append("ObjPrintMange.PrintReport('").append(readTplFile()).append("','").append(prtParaName).append("','").append(prtParaValue).
                    append("'");
        } else {
            sb.append("ObjPrintMange.ShowReport('").append(readTplFile()).append("','").append(prtParaName).append("','").append(prtParaValue).
                    append("'");
        }
        for (int i = 1; i < 7; i++) {
            String sl = Conf.getBaseProp(sid, "prt.v" + i + ".sql");
            if (TL.isEmpty(sl)) {
                sb.append(", ''");
            } else {
                String sp = Conf.getBaseProp(sid, "prt.v" + i + ".sql.para");
                if (TL.isNotEmpty(sp)) {
                    String xv, xx[] = sp.split(",");
                    for (String x : xx) {
                        if (TL.isEmpty(x)) {
                            continue;
                        }
                        xv = request.getParameter(x);
                        if (TL.isEmpty(xv)) {
                            xv = "";
                        }
                        sl = sl.replaceAll(":" + x, xv);
                    }
                }
                request.getParameter(sp);
                sb.append(",'").append(HSF.queryAndRuPrtXml(dbl,sl)).append("'");
            }
        }
        sb.append(");ObjPrintMange = null; } </script></body></html>");
        return sb.toString();
    }

    private String readTplFile() {
        StringBuilder sb = new StringBuilder();
        try {
            if (TL.isEmpty(prtLoca)) {
                prtLoca = PrtBuzz.class.getResource("/prop/tpl/" + sid + ".fr3").getPath();
            } else {
                prtLoca = prtLoca + "/" + sid + ".fr3";
            }
            FileReader fr = new FileReader(prtLoca);
            BufferedReader br = new BufferedReader(fr);
            do {
                sb.append(br.readLine());
            } while (br.ready());
        } catch (IOException ex) {
        }
        return sb.toString();
    }
}
